using System;
using System.Collections;
using System.Data.SqlClient;

using LocalBoard.Domain;
using LocalBoard.Persistence;

using NUnit.Framework;

namespace LocalBoard.Controller
{
	/// <summary>
	/// Summary description for TestAdminController.
	/// </summary>
	[TestFixture]
	public class TestAdminController
	{
		private AdminController _unitUnderTest = null;
		IAccessorFactory _accessorFactory = null;
		private IProfileAccessor _profileAccessor = null;

		[SetUp]
		public void setUp()
		{
			_accessorFactory = new MockAccessorFactory();
			_profileAccessor = _accessorFactory.GetProfileAccessor();
			_unitUnderTest = new AdminController(_accessorFactory);
		}

		[TearDown]
		public void tearDown()
		{
			Profile user = _profileAccessor.GetProfileByUserId("userId5");
			if(user != null)
				_profileAccessor.Delete(user.Id);

			MockAccessorFactory.ResetDataTable();

			_unitUnderTest = null;
			_accessorFactory = null;
			_profileAccessor = null;
		}

		[Test]
		public void testCreateUserWithDuplicateId()
		{
			if(_profileAccessor.GetProfileByUserId("userId1") == null)
				_profileAccessor.Insert(new Profile("userId1", "userName1", "password1", "email1"));
			try
			{
				_unitUnderTest.CreateUser(new Profile("userId1", "userName1", "password1", "email1"));
				Assert.Fail("Duplicate userId should generate exception");
			}
			catch(SqlException)
			{}
			catch(ApplicationException)
			{}
		}

		[Test]
		public void testCreateUser()
		{
			Profile user = _profileAccessor.GetProfileByUserId("userId5");
			if(user != null)
				_profileAccessor.Delete(user.Id);
			int userId = _unitUnderTest.CreateUser(new Profile("userId5", "userName5", "password5", "email5"));
			Assert.IsTrue(userId != 0);
		}

		[Test]
		public void testGetUser()
		{
			Profile user = _profileAccessor.GetProfileByUserId("userId5");
			int userId;
			if(user == null)
				userId = _profileAccessor.Insert(new Profile("userId1", "userName1", "password1", "email1"));
			else
				userId = user.Id;

			user = _unitUnderTest.GetUser(userId);
			Assert.IsNotNull(user);
			Assert.AreEqual(userId, user.Id);
		}

		[Test]
		public void testUpdateUser()
		{
			Profile profile = Profile.CreateProfile("testUser", "testUser", "testUser", "testUser@test.com", "/images/photo/test.jpg", "testInfo");
			profile = _profileAccessor.Get(_profileAccessor.Insert(profile));
			profile.UserId = "updatedUser";
			profile.UserName = "updatedUser";
			_unitUnderTest.UpdateUser(profile);
		}
		[Test]
		public void testGetUserRole()
		{
			string testRoleStr = "testRole";
			int roleId = _accessorFactory.GetUserRoleAccessor().Insert(testRoleStr);
			UserRole role = _unitUnderTest.GetUserRole(roleId);
			UserRole role2 = _unitUnderTest.GetUserRole(testRoleStr);
			Assert.AreEqual(roleId, role.Id);
			Assert.AreEqual(role.Name, role2.Name);
		}

		[Test]
		public void testCreateUserRole()
		{
			string testRoleStr = "testRole";
			int testRoleId = _unitUnderTest.CreateUserRole(testRoleStr);
			UserRole role = _unitUnderTest.GetUserRole(testRoleId);
			Assert.AreEqual(testRoleStr, role.Name);
		}

		[Test]
		public void testAssignUserRoleToUser()
		{
			int profileId = _unitUnderTest.CreateUser(new Profile("userId", "user name", "pasword", "test@test.com"));
			int testRoleId = _unitUnderTest.CreateUserRole("testRole");
			_unitUnderTest.AssignUserRoleToUser(testRoleId, profileId);
			Profile user = _unitUnderTest.GetUser(profileId);
			Assert.IsTrue(user.IsInRole(testRoleId));
		}

		[Test]
		public void testCreateClub()
		{
			int clubId = _unitUnderTest.CreateClub(new Club("testClub", "test club", 1, "test", "", null));
			Club club = _unitUnderTest.GetClub(clubId);
			Assert.AreEqual(clubId, club.Id);
			Assert.AreEqual("testClub", club.UniqueName);
		}

		[Test]
		public void testGetClub()
		{
			string clubUniqueName = "testClub";
			int clubId = _unitUnderTest.CreateClub(new Club(clubUniqueName, "test club", 1, "test", "", null));
			Club club = _unitUnderTest.GetClub(clubId);
			Club clubAgain = _unitUnderTest.GetClub(clubUniqueName);
			Assert.AreEqual(clubId, club.Id);
			Assert.AreEqual(club.UniqueName, clubAgain.UniqueName);
		}

		[Test]
		public void testGetUserByUserIdAndPassword()
		{
			string userid = "userId1";
			string password = "password1";
			int oid = _profileAccessor.Insert(new Profile(userid, "userName1", password, "email1"));
			Profile user = _unitUnderTest.GetUserByUserIdAndPassword(userid, password);
			Assert.IsNotNull(user);
			Assert.AreEqual(oid, user.Id);

			Profile user2 = _unitUnderTest.GetUserByUserIdAndPassword(userid, "test");
			Assert.IsNull(user2);

			Profile user3 = _unitUnderTest.GetUserByUserIdAndPassword("userid", password);
			Assert.IsNull(user3);
		}

		[Test]
		public void testCreateClubCategoryAndGetAllClubCategories()
		{
			int clubCategoryId = _unitUnderTest.CreateClubCategory("testClubCategory");
			Assert.AreEqual("testClubCategory", _unitUnderTest.GetAllClubCategories()[clubCategoryId].ToString());
		}

		[Test]
		public void testDeleteClubCategory()
		{
			int clubCategoryId = _unitUnderTest.CreateClubCategory("testClubCategory");
			int clubCategoryId2 = _unitUnderTest.CreateClubCategory("testClubCategory2");
			int clubCategoryCount = _unitUnderTest.GetAllClubCategories().Count;
			_unitUnderTest.DeleteClubCategory(clubCategoryId);
			Assert.AreEqual(clubCategoryCount - 1, _unitUnderTest.GetAllClubCategories().Count);
		}

		[Test]
		public void testUpdateClubCategory()
		{
			int clubCategoryId = _unitUnderTest.CreateClubCategory("testClubCategory");
			_unitUnderTest.UpdateClubCategory("testClubCategory2", clubCategoryId);
			Assert.AreEqual("testClubCategory2", _unitUnderTest.GetAllClubCategories()[clubCategoryId].ToString());
		}
	}
}